<?php

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class HT_Model_administrator_models_icd10 extends Zend_Db_Table {//ten class fai viet hoa

	protected $_db;

	public function __construct() {
		$this->_name = "icd10";
		$this->_db = Zend_Registry::get('dbMain');
		parent::init();
	}
	
	public function addData($data){
		$icd_code = trim($data['icd_code']);
		if(!$this->checkExistsIcdCode($icd_code)){
			$this->insert($data);
			return $this->getMaxId();
		}else{
			return "-1";
		}
	}
	
	public function updateData($data,$icd_id){
		$icd_code = trim($data['icd_code']);
		if(!$this->checkExistsIcdCode($icd_code,$icd_id,'update')){
			$this->update($data,'icd_id='.(int)$icd_id);
			return $icd_id;
		}else{
			return "-1";
		}
	}
	
	public function checkExistsIcdCode($icd_code,$icd_id = null,$type = 'add'){
		if($type == 'add'){
			$sql = "SELECT COUNT(icd_id) FROM icd10 WHERE icd_code = ?";
			return  (int)$this->_db->fetchOne($sql,array($icd_code));
		}else{
			$sql = "SELECT COUNT(icd_id) FROM icd10 WHERE icd_code = ? AND icd_id != ?";
			return  (int)$this->_db->fetchOne($sql,array($icd_code,$icd_id));
		}
	}
	
	public function getMaxId(){
		$sql = "SELECT MAX(icd_id) FROM icd10";
		return  (int)$this->_db->fetchOne($sql);
	}
	public function getIcd10($icd10Id,$filter = array()) {
		$sqlPlus = $this->getListIcd10_sqlPlus($filter);
		$sql = "SELECT icd.*, grp.group_name,ich.chapter_id, ich.chapter_name
				FROM icd10 icd
				INNER JOIN icd_group grp ON icd.group_id = grp.group_id
				INNER JOIN icd_chapter ich ON grp.chapter_id = ich.chapter_id
				WHERE icd_id= ".(int)$icd10Id;
		return $this->_db->fetchRow($sql);
	}
	public function getListIcd10_nb($filter = array()) {
		$sqlPlus = $this->getListIcd10_sqlPlus($filter);
		$sql = "SELECT COUNT(icd.icd_id)
				FROM icd10 icd
				INNER JOIN icd_group grp ON icd.group_id = grp.group_id
				INNER JOIN icd_chapter ich ON grp.chapter_id = ich.chapter_id
				WHERE 1=1 $sqlPlus";
		return $this->_db->fetchOne($sql);
	}
	public function getListIcd10($start=0,$size = 10,$filter = array()) {
		$sqlPlus = $this->getListIcd10_sqlPlus($filter);
		$sql = "SELECT icd.*, grp.group_name, ich.chapter_name
				FROM icd10 icd
				INNER JOIN icd_group grp ON icd.group_id = grp.group_id
				INNER JOIN icd_chapter ich ON grp.chapter_id = ich.chapter_id
				WHERE 1=1 $sqlPlus ORDER BY icd_name_vn ASC LIMIT $start,$size";
		return $this->_db->fetchAll($sql);
	}
	
	private function getListIcd10_sqlPlus($filter){
		$sqlPlus = null;
		foreach((array)$filter as $key => $val){
			$key = trim($key);
			$val = trim($val);
			switch($key){
				case 'keyword':
					if($val) $sqlPlus .= " AND (icd_code LIKE '%$val%' OR icd_name_vn LIKE '%$val%') ";
					break;
				case 'chapter_id':
					if($val >0) $sqlPlus .= " AND ich.chapter_id = ".(int)$val;
					break;
				case 'group_id':
					if($val >0) $sqlPlus .= " AND grp.group_id = ".(int)$val;
					break;
			}
		}
		return $sqlPlus;
	}
	
}

?>
